package com.zmn.oms.admin.controller.sync;

import com.google.common.collect.Maps;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.oms.admin.controller.OmsBaseController;
import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.common.constant.OrderStatusConsts;
import com.zmn.oms.common.dto.ResultDTO;
import com.zmn.oms.model.dto.work.modify.other.ZsPartReimburseGroupDTO;
import com.zmn.oms.model.entity.part.OrderPart;
import com.zmn.oms.model.entity.work.OrderWork;
import com.zmn.oms.services.interfaces.part.OrderPartService;
import com.zmn.oms.services.interfaces.work.OrderWorkService;
import com.zmn.oms.zmn.normal.business.interfaces.order.ZsNormalOrderBService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;


/**
 * 类描述：配件自动结算
 * @author heciqi
 * @date 2021/8/18 15:58
 */
@Slf4j
@RequestMapping("/sync")
@RestController
public class SyncPartSettController extends OmsBaseController {
    @Autowired
    private ZsNormalOrderBService zsNormalOrderBService;
    @Autowired
    private OrderWorkService orderWorkService;
    @Autowired
    private OrderPartService orderPartService;

	/**
	 *
	 * /sync/partsett?workIds=xxx
	 * @param workIds
	 * @return
	 */
    @GetMapping(value = "/partsett")
    public ResultDTO partsett(String workIds) {
        log.info("SyncPartSettController#partsett：{}", workIds);
        if (StringUtil.isBlank(workIds)) {
            return ResultDTO.fail("参数有误");
        }

        String[] arrWorkId = workIds.split(",");
        List<Long> workIdList = Arrays.stream(arrWorkId).map(Long::parseLong).collect(Collectors.toList());
        List<OrderWork> orderWorks = orderWorkService.listOrderWorkByOrderIdList(workIdList);
        Map<Long, OrderWork> orderWorkMap = Maps.uniqueIndex(orderWorks, OrderWork::getWorkId);

        List<OrderPart> orderPartList = orderPartService.listEsOrderPartByOrderIdList(workIdList);
        Map<Long, List<OrderPart>> orderPartMap = orderPartList.stream().collect(Collectors.groupingBy(OrderPart::getOrderId));

        for (Long workId : workIdList) {
            OrderWork orderWork = orderWorkMap.get(workId);
            List<OrderPart> orderParts = orderPartMap.get(workId);
            Integer reimburseStatus = orderWork.getPartReimburseStatus();

            log.info("开始处理==>workId：{}，partReimburseStatus:{}", workId, reimburseStatus);
            ZsPartReimburseGroupDTO partReimburseGroupDTO = new ZsPartReimburseGroupDTO();
            partReimburseGroupDTO.setOrderId(workId);
            partReimburseGroupDTO.setWorkId(workId);
            partReimburseGroupDTO.setOperator("系统");
            log.info("开始调用erp报销队列==>{}", workId);

            if (Objects.equals(reimburseStatus, OrderConsts.PART_REIMBURSE_STATUS_SUCCESS)) {
                logger.info("{}报销成功发送所有配件到erp", partReimburseGroupDTO.getOrderId());
                zsNormalOrderBService.sendPartReimburseInfoToERP(orderWork, orderWork, partReimburseGroupDTO, orderParts);
            } else if (Objects.equals(reimburseStatus, OrderConsts.PART_REIMBURSE_STATUS_FAIL)) {
                logger.info("{}报销失败只发送内采配件到erp", partReimburseGroupDTO.getOrderId());
                List<OrderPart> innerOrderParts = orderParts.stream()
                        .filter(e -> Objects.equals(e.getPartType(), OrderConsts.PART_TYPE_SOURCING_IN))
                        .collect(Collectors.toList());
                if (CollectionUtils.isNotEmpty(innerOrderParts)) {
                    zsNormalOrderBService.sendPartReimburseInfoToERP(orderWork, orderWork, partReimburseGroupDTO, innerOrderParts);
                }
            } else if (Objects.equals(reimburseStatus, OrderConsts.PART_REIMBURSE_STATUS_NO)) {
                logger.info("{}不予报销", partReimburseGroupDTO.getOrderId());
                if (orderWork.getStatus() >= OrderStatusConsts.WORK_STATUS_ACCOUNT) {
                    logger.info("{}不予报销已算账-才发送内采配件到erp", partReimburseGroupDTO.getOrderId());
                    List<OrderPart> innerOrderParts = orderParts.stream()
                            .filter(e -> Objects.equals(e.getPartType(), OrderConsts.PART_TYPE_SOURCING_IN))
                            .collect(Collectors.toList());
                    if (CollectionUtils.isNotEmpty(innerOrderParts)) {
                        zsNormalOrderBService.sendPartReimburseInfoToERP(orderWork, orderWork, partReimburseGroupDTO, innerOrderParts);
                    }
                }
            } else {
                logger.error("未知报销状态{}", reimburseStatus);
            }

        }
        return ResultDTO.success("配件报销调用队列成功");
    }


}
